#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "tp2.h"

  void RemoveSK(char titulo[200], char buffer[200], PK *listaSK, int *nSK, FILE *linvert, FILE *avail)
  {
    PK *found;
    char verifica[200],espaco=' ';
    int NRR,navail,NRRant,aux;
    found=BSearch(listaSK,buffer,0,*(nSK)-1);
    NRR=found->NRR;
    fseek(linvert,NRR*204,0);
    fread(verifica,1,tmtitulo,linvert);
    if(VerificaIdent(titulo,verifica)==SAME){
      *found=listaSK[(*nSK)-1];
      (*nSK)--;
      if((*nSK)%20==0){
        listaSK=(PK*)realloc(listaSK,(*nSK)*sizeof(PK));
      }
      Ordena(listaSK,0,(*nSK)-1);
      fread(&navail,1,4,avail);
      fseek(linvert,(NRR*204),0);
      fwrite(&espaco,1,1,linvert);
      fwrite(&navail,1,4,linvert);
      fseek(avail,0,0);
      fwrite(&NRR,1,4,avail);
      return;
    }
    while(VerificaIdent(titulo,verifica)!=SAME){
      NRRant=NRR;
      fread(&NRR,1,4,linvert);
      fseek(linvert,NRR*204,0);
      fread(verifica,1,tmtitulo,linvert);
    }
    fseek(linvert,(NRR*204)+200,0);
    fread(&aux,1,4,linvert);
    fseek(linvert,(NRRant*204)+200,0);
    fwrite(&aux,1,4,linvert);
    fread(&navail,1,4,avail);
    fseek(linvert,(NRR*204),0);
    fwrite(&espaco,1,1,linvert);
    fwrite(&navail,1,4,linvert);
    fseek(avail,0,0);
    fwrite(&NRR,1,4,avail);
    return;
  }

  void RemovePKeBase(PK *lista, PK *remove, int *nreg){
    char buffer[200], espaco=' ';
    int i=0, NRR;
    FILE *file, *avail;
    file=fopen("base20.dat","r+");
    avail=fopen("availbase.dat","r+");
    /*removendo da base*/
    fseek(file,(remove->NRR)*450,0);
    fwrite(&espaco,1,1,file);
    fread(&NRR,1,4,avail);
    fwrite(&NRR,1,4,file);
    fwrite(&(remove->NRR),1,4,avail);
    fclose(file);
    fclose(avail);
    /*removendo da PK*/
    for(i=0;i<200;i++){
      remove->titulo[i]=lista[(*nreg)-1].titulo[i];
    }
    remove->NRR=lista[(*nreg)-1].NRR;
    (*nreg)--;
    if((*nreg)%20==0){
      lista=(PK*)realloc(lista,(*nreg)*sizeof(PK));
    }
    Ordena(lista,0,(*nreg)-1);
    return;
  }

  void RemoveKeyTitulo(PK *lista, char *titulo, int *nreg, int tamreg)
  {
    char buffer[200];
    int i=0, b=0;
    FILE *linvert, *avail;
    while((i<tamreg)&&(titulo[i]!=' ')){
      while((i<tamreg)&&(titulo[i]!=' ')){
        buffer[b]=titulo[i];
        i++;
        b++;
      }
      i++;
      while(b<200){
        buffer[b]=' ';
        b++;
      }
      printf("\n");
      linvert=fopen("listatitulo.dat","r+");
      avail=fopen("availtitulo.dat","r+");
      RemoveSK(titulo,buffer,lista,nreg,linvert,avail);
      fclose(linvert);
      fclose(avail);
      b=0;
    }
    return;
  }

  void RemoveKeyTipo(PK *lista, char *titulo, char *chave, int *nreg, int tamreg)
  {
    char buffer[200];
    int i=0, b=0;
    FILE *linvert, *avail;
    while((i<tamreg)&&(chave[i]!=' ')){
      while((i<tamreg)&&(chave[i]!=' ')){
        buffer[i]=chave[i];
        i++;
      }
      b=i;
      i++;
      while(b<200){
        buffer[b]=' ';
        b++;
      }
      linvert=fopen("listatipo.dat","r+");
      avail=fopen("availtipo.dat","r+");
      RemoveSK(titulo,buffer,lista,nreg,linvert,avail);
      fclose(linvert);
      fclose(avail);
    }
    return;
  }
  
  void RemoveKeyAutor(PK *lista, char *titulo, char *chave, int *nreg, int tamreg)
  {
    char buffer[200];
    int i=0, b=0;
    FILE *linvert, *avail;
    while((i<tamreg)&&(chave[i]!=' ')){
      while((i<tamreg)&&(chave[i]!=' ')){
        buffer[i]=chave[i];
        i++;
      }
      b=i;
      i++;
      while(b<200){
        buffer[b]=' ';
        b++;
      }
      linvert=fopen("listaautor.dat","r+");
      avail=fopen("availautor.dat","r+");
      RemoveSK(titulo,buffer,lista,nreg,linvert,avail);
      fclose(linvert);
      fclose(avail);
    }
    return;
  }
  
  void RemoveKeyAno(PK *lista, char *titulo, char *chave, int *nreg, int tamreg)
  {
    char buffer[200];
    int i=0, b=0;
    FILE *linvert, *avail;
    while((i<tamreg)&&(chave[i]!=' ')){
      while((i<tamreg)&&(chave[i]!=' ')){
        buffer[i]=chave[i];
        i++;
      }
      b=i;
      i++;
      while(b<200){
        buffer[b]=' ';
        b++;
      }
      linvert=fopen("listaano.dat","r+");
      avail=fopen("availano.dat","r+");
      RemoveSK(titulo,buffer,lista,nreg,linvert,avail);
      fclose(linvert);
      fclose(avail);
    }
    return;
  }
